package com.sunyur.tailor.channel.service.inner.recipients.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.sunyur.tailor.channel.common.inner.base.SunyurListResult;
import com.sunyur.tailor.channel.common.inner.base.SunyurResult;
import com.sunyur.tailor.channel.common.inner.util.SunyurClientUtil;
import com.sunyur.tailor.channel.common.inner.util.SunyurResultUtil;
import com.sunyur.tailor.channel.common.middle.base.ChannelListResult;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.enums.MiddleResultEnum;
import com.sunyur.tailor.channel.common.middle.util.ChannelListResultUtil;
import com.sunyur.tailor.channel.common.middle.util.ChannelResultUtil;
import com.sunyur.tailor.channel.service.inner.recipients.model.RecipientsHeadModel;
import com.sunyur.tailor.channel.service.inner.recipients.model.RecipientsItemModel;
import com.sunyur.tailor.channel.service.inner.recipients.param.RecipientsConfirmParam;
import com.sunyur.tailor.channel.service.inner.recipients.param.RecipientsHeadParam;
import com.sunyur.tailor.channel.service.inner.recipients.param.RecipientsOutsideAuditParam;
import com.sunyur.tailor.channel.service.inner.recipients.param.RecipientsRollBackStatusParam;
import com.sunyur.tailor.channel.service.inner.recipients.service.RecipientService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @Author: HuBaoding
 * @Description:
 * @Date: Created in 11:36 2021/5/14
 */
@Service("recipientService")
public class RecipientServiceImpl implements RecipientService {

    private static final Logger LOGGER_INFO = LoggerFactory.getLogger(RecipientServiceImpl.class);
    private static final Logger LOGGER_ERROR = LoggerFactory.getLogger(RecipientServiceImpl.class);

    /**
     * 请求商越Client
     */
    @Autowired
    private SunyurClientUtil sunyurClient;

    @Value("${sunyur.api.purchaseApplyRollBackStatus}")
    private String purchaseApplyRollBackStatus;

    @Value("${sunyur.api.recipientsListItem}")
    private String recipientsListItem;

    @Value("${sunyur.api.recipientsOutsideAudit}")
    private String recipientsOutsideAudit;

    @Value("${sunyur.api.recipientsConfirm}")
    private String recipientsConfirm;

    @Value("${sunyur.api.recipientsRollBackStatus}")
    private String recipientsRollBackStatus;




    /**
     * 根据单号获取领用单头部信息
     * @param param
     * @return
     */
    @Override
    public ChannelResult<RecipientsHeadModel> getRecipientHead(RecipientsHeadParam param) {
        SunyurResult<RecipientsHeadModel> sunyurResult=null;
        try {
            sunyurResult = sunyurClient.execPost(purchaseApplyRollBackStatus, JSON.toJSONString(param), new TypeReference<SunyurResult<RecipientsHeadModel>>() {});
            // 主线返回错误
            if (!SunyurResultUtil.isSuccess(sunyurResult)) {
                return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
            }
        } catch (Exception e) {
            LOGGER_ERROR.error("RecipientServiceImpl getRecipientHead fail,param is {} ",JSON.toJSONString(param));
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(),e.getMessage());
        }
        LOGGER_INFO.info("RecipientServiceImpl getRecipientHead param is {} , result is {}",JSON.toJSONString(param),JSON.toJSONString(sunyurResult));
        return ChannelResultUtil.success(sunyurResult.getContent());
    }

    /**
     * 根据单号获取领用行信息
     * @param param
     * @return
     */
    @Override
    public ChannelListResult<RecipientsItemModel> listItem(RecipientsHeadParam param) {
        SunyurListResult<RecipientsItemModel> sunyurListResult =null;
        try {
            sunyurListResult = sunyurClient.execListPost(recipientsListItem, JSON.toJSONString(param),
                    new TypeReference<SunyurListResult<RecipientsItemModel>>() {});
            // 主线返回错误
            if (!SunyurResultUtil.isSuccess(sunyurListResult)) {
                return ChannelListResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurListResult.getErrorMessage());
            }
        } catch (Exception e) {
            LOGGER_ERROR.error("RecipientServiceImpl listItem fail,param is {} ",JSON.toJSONString(param));
            return ChannelListResultUtil.error(MiddleResultEnum.FAILED.getCode(),e.getMessage());
        }
        LOGGER_INFO.info("RecipientServiceImpl listItem param is {} , result is {}",JSON.toJSONString(param),JSON.toJSONString(sunyurListResult));
        return ChannelListResultUtil.success(sunyurListResult.getContent());
    }

    /**
     * 领用单外部审批
     * @param param
     * @return
     */
    @Override
    public ChannelResult<Boolean> recipientsOutsideAudit(RecipientsOutsideAuditParam param) {
        SunyurResult<Boolean> sunyurResult=null;
        try {
            sunyurResult = sunyurClient.execPost(recipientsOutsideAudit, JSON.toJSONString(param), new TypeReference<SunyurResult<Boolean>>() {});
            // 主线返回错误
            if (!SunyurResultUtil.isSuccess(sunyurResult)) {
                return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
            }
        } catch (Exception e) {
            LOGGER_ERROR.error("RecipientServiceImpl recipientsOutsideAudit fail,param is {} ",JSON.toJSONString(param));
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(),e.getMessage());
        }
        LOGGER_INFO.info("RecipientServiceImpl recipientsOutsideAudit param is {} , result is {}",JSON.toJSONString(param),JSON.toJSONString(sunyurResult));
        return ChannelResultUtil.success(sunyurResult.getContent());
    }

    /**
     * 确认领用
     * @param param
     * @return
     */
    @Override
    public ChannelResult<Boolean> recipientsConfirm(RecipientsConfirmParam param) {
        SunyurResult<Boolean> sunyurResult=null;
        try {
            sunyurResult = sunyurClient.execPost(recipientsConfirm, JSON.toJSONString(param), new TypeReference<SunyurResult<Boolean>>() {});
            // 主线返回错误
            if (!SunyurResultUtil.isSuccess(sunyurResult)) {
                return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
            }
        } catch (Exception e) {
            LOGGER_ERROR.error("RecipientServiceImpl recipientsConfirm fail,param is {} ",JSON.toJSONString(param));
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(),e.getMessage());
        }
        LOGGER_INFO.info("RecipientServiceImpl recipientsConfirm param is {} , result is {}",JSON.toJSONString(param),JSON.toJSONString(sunyurResult));
        return ChannelResultUtil.success(sunyurResult.getContent());
    }

    /**
     * 领用单状态回滚
     * @param param
     * @return
     */
    @Override
    public ChannelResult<Boolean> recipientsRollBackStatus(RecipientsRollBackStatusParam param) {
        SunyurResult<Boolean> sunyurResult=null;
        try {
            sunyurResult = sunyurClient.execPost(recipientsRollBackStatus, JSON.toJSONString(param), new TypeReference<SunyurResult<Boolean>>() {});
            // 主线返回错误
            if (!SunyurResultUtil.isSuccess(sunyurResult)) {
                return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
            }
        } catch (Exception e) {
            LOGGER_ERROR.error("RecipientServiceImpl recipientsRollBackStatus fail,param is {} ",JSON.toJSONString(param));
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(),e.getMessage());
        }
        LOGGER_INFO.info("RecipientServiceImpl recipientsRollBackStatus param is {} , result is {}",JSON.toJSONString(param),JSON.toJSONString(sunyurResult));
        return ChannelResultUtil.success(sunyurResult.getContent());
    }


}
